<?php

namespace  app\man\controller;

use think\Controller;
use think\Db;
use think\Paginator;
use think\Loader;
use think\Request;
use ucloud\proxy;
use think\Cache;
class Article extends Common
{
	public function  index()
	{
 		$page=input('page',1);
 		$where=array();
 		//if(Request::instance()->isPost()){
 			$aid = intval(input('aid'));
	 		$ntitle = input('ntitle');
	 		if($aid!=''){
	 			$where['id']=$aid;
	 		}
	 		if($ntitle!=''){
	 			$where['ntitle']=['like','%'.$ntitle.'%'];
	 		}
 		//}
 		 
        $count = Db::name('news')->where($where)->count();
        $list =  Db::name('news')->where($where)->order('nshunxu desc')->paginate(15,false,[
        		'page'=>$page,
        		//'path'=>'[PAGE].html',
        	]);
        $this->assign('list', $list);
		$data=$this->fetch('index');
		echo $data;
	}

	public function  add()
	{
		 
		if(Request::instance()->isPost()){
			if(!$config=cache::get('config')){
				$config = Db::name('sys_config')->where('id',1)->find();
				cache::store("redis")->set('config',$config);
			}
			vendor('ucloud.proxy');
	        $data = Request::instance()->post();
	        $data['ntime'] = date('Y-m-d H:i:s');
	        if ($_FILES['pic']['tmp_name'] != '') {

	            $file = request()->file('pic');
	            $path=ROOT_PATH . 'public'.DS.'uploads/';
	            $info = $file->validate(['size'=>25165824,'ext'=>'jpg,png,gif'])->move($path);
	            
	            if (!$info) {
	                $this->error($file->getError());
	            } else {
	                $data['nimg'] = $info->getSaveName();
	                $data['nimg'] = substr($data['nimg'],9);
	            }
	            UCloud_PutFile($config['bucket'],$data['nimg'],$path.date('Ymd').'/'.$data['nimg']);
	        }
	        $result = Db::name('news')->insert($data);
	        if ($result) {
	            $this->success('添加成功！', url('man/article/index'));
	        } else {
	            $this->error('添加失败！');
	        }
		}
		$fenlei = Db::name('fenlei')->field('id,fname')->select();
		$types = [
			['id'=>1,'fname'=>'普通'],
			['id'=>2,'fname'=>'分数']
		];
		$this->assign('fenlei',$fenlei);
		$this->assign('types',$types);
		return $this->fetch('add');
	}

	public function  edit()
	{
		$aid = input('id');
		if(Request::instance()->isPost()){
			if(!$config=cache::get('config')){
				$config = Db::name('sys_config')->where('id',1)->find();
				cache::store("redis")->set('config',$config);
			}
			vendor('ucloud.proxy');
			$data=Request::instance()->post();
	
			if ($_FILES['pic']['tmp_name'] != '') {

	            $file = request()->file('pic');

	            $path=ROOT_PATH . 'public'.DS.'uploads/';

	            $info = $file->validate(['size'=>25165824,'ext'=>'jpg,png,gif'])->move($path);
	            
	            if (!$info) {
	                $this->error($file->getError());
	            } else {
	                $data['nimg'] = $info->getSaveName();
	                $data['nimg'] = substr($data['nimg'],9);
	            }
	            UCloud_PutFile($config['bucket'],$data['nimg'],$path.date('Ymd').'/'.$data['nimg']);
	        }
	        $res=Db::name('news')->where('id',$aid)->update($data);
	        if($res){
	        	cache::rm('view-'.$aid);
				$this->success('修改成功',url('man/article/index'));
			}else{
				$this->success('修改失败',url('man/article/index'));
			}
		}
		$art = Db::name('news')->where('id',$aid)->find();
		$this->assign('art',$art);
		$fenlei = Db::name('fenlei')->field('id,fname')->select();
		$types = [
			['id'=>1,'fname'=>'普通'],
			['id'=>2,'fname'=>'分数']
		];
		$this->assign('fenlei',$fenlei);
		$this->assign('types',$types);
		return $this->fetch('edit');
	}

	public function addcate(){
		if(Request::instance()->isPost()){
			$data=Request::instance()->post();
			$data['ptime']=time();
			$res = Db::name('fenlei')->insert($data);
			if($res){
				$this->success('添加成功！',url('man/article/category'));
			}else{
				$this->success('添加失败！',url('man/article/category'));
			}
		}
		return $this->fetch('addcate');
	}

	public function category()
	{
		$cate = Db::name('fenlei')->select();
		$this->assign('cate',$cate);
		return $this->fetch('category');
	}

	public function editcate(){
		$id = input('aid');
		if(Request::instance()->isPost()){
			$data=Request::instance()->post();
			$data['ptime']=time();
			$res=Db::name('fenlei')->where('id',$id)->update($data);
	        if($res){
				$this->success('修改成功',url('man/article/category'));
				cache::rm("category");
			}else{
				$this->success('修改失败',url('man/article/category'));
			}
		}
		$data =Db::name('fenlei')->where('id',$id)->find();
		$this->assign('data',$data);
		return $this->fetch('editcate');
	}

	public function delete(){
		$id = input('aid');
		$type=input('type');
		if($type==1){
			$table='news';
		}elseif($type==2){
			$table='news_item';
		}else{
			$table='news_result';
		}
		$res = Db::name($table)->where('id',$id)->delete();

		if($res){
			return true;
		}else{
			return false;
		}
	}

	public function item(){
		$map['nid']=input('nid');
		$map['nceng']=input('nceng');
		 
		
		$item = Db::name('news_item')->where($map)->order('ishunxu,id asc')->select();

		$this->assign('item',$item);
		if($map['nceng']==2){
			$map['nnid']=input('nnid');
		}
		$this->assign('map',$map);

		return $this->fetch('item');
	}

	public function edititem(){
		$id = input('id');
		$map['nid']=input('nid');
		$map['nceng']=input('nceng');
		if(Request::instance()->isPost()){
			if(!$config=cache::get('config')){
				$config = Db::name('sys_config')->where('id',1)->find();
				cache::store("redis")->set('config',$config);
			}
			vendor('ucloud.proxy');
			$data=Request::instance()->post();
			if ($_FILES['pic']['tmp_name'] != '') {

	            $file = request()->file('pic');

	            $path=ROOT_PATH . 'public'.DS.'uploads/';

	            $info = $file->validate(['size'=>25165824,'ext'=>'jpg,png,gif'])->move($path);
	            
	            if (!$info) {
	                $this->error($file->getError());
	            } else {
	                $data['iimage'] = $info->getSaveName();
	                $data['iimage'] = substr($data['iimage'],9);
	            }
	            UCloud_PutFile($config['bucket'],$data['iimage'],$path.date('Ymd').'/'.$data['iimage']);
	        }
	        $data['itime']=time();
	        $res=Db::name('news_item')->where('id',$id)->update($data);
	        if($res){
				$this->success('修改成功',url('man/article/item',['nid'=>$data['nid'],'nceng'=>$data['nceng']]));
			}else{
				$this->success('修改失败',url('man/article/item',['nid'=>$data['nid'],'nceng'=>$data['nceng']]));
			}
		}
		$item = Db::name('news_item')->where('id',$id)->find();

		$this->assign('item',$item);
		$this->assign('map',$map);
		return $this->fetch('edititem');
	}

	public function additem(){
		$map['nid']=input('nid');
		$map['nceng']=input('nceng');
		if($map['nceng']==2){
			$map['nnid']=input('nnid');
		}
		if(Request::instance()->isPost()){
			if(!$config=cache::get('config')){
				$config = Db::name('sys_config')->where('id',1)->find();
				cache::store("redis")->set('config',$config);
			}
			vendor('ucloud.proxy');
			$data = Request::instance()->post();
			if ($_FILES['pic']['tmp_name'] != '') {

	            $file = request()->file('pic');

	            $path=ROOT_PATH . 'public'.DS.'uploads/';

	            $info = $file->validate(['size'=>25165824,'ext'=>'jpg,png,gif'])->move($path);
	            
	            if (!$info) {
	                $this->error($file->getError());
	            } else {
	                $data['iimage'] = $info->getSaveName();
	                $data['iimage'] = substr($data['iimage'],9);
	            }
	            UCloud_PutFile($config['bucket'],$data['iimage'],$path.date('Ymd').'/'.$data['iimage']);
	        }
			$data['itime']=time();
			if($data['nceng']==2){
				$nnid=$data['nnid'];
				unset($data['nnid']);
			}
			$res=Db::name('news_item')->insert($data);
			if($res){
				if($data['nceng']==2){
					$this->success('添加成功',url('man/article/item',['nid'=>$data['nid'],'nnid'=>$nnid,'nceng'=>$data['nceng']]));
				}else{
					$this->success('添加成功',url('man/article/item',['nid'=>$data['nid'],'nceng'=>$data['nceng']]));
				}
				
			}else{
				$this->success('添加失败',url('man/article/item',['nid'=>$data['nid'],'nceng'=>$data['nceng']]));
			}
		}
		
		$this->assign('map',$map);

		return $this->fetch('additem');
	}

	public function answer(){
		$map['nid']=input('nid');

		$item = Db::name('news_result')->where($map)->order('id asc')->select();

		$this->assign('item',$item);

		$this->assign('map',$map);

		return $this->fetch('answer');
	}

	public function addanswer(){
		$map['nid']=input('nid');
	 
		if(Request::instance()->isPost()){
			$data = Request::instance()->post();
			$res=Db::name('news_result')->insert($data);
			if($res){
				$this->success('添加成功',url('man/article/answer',['nid'=>$data['nid']]));
			}else{
				$this->success('添加失败',url('man/article/answer',['nid'=>$data['nid']]));
			}
		}
		
		$this->assign('map',$map);

		return $this->fetch('addanswer');
	}

	public function editanswer(){
		$id = input('id');
		$map['nid']=input('nid');
		if(Request::instance()->isPost()){
			$data=Request::instance()->post();
	        $res=Db::name('news_result')->where('id',$id)->update($data);
	        if($res){
				$this->success('修改成功',url('man/article/answer',['nid'=>$data['nid']]));
			}else{
				$this->success('修改失败',url('man/article/answer',['nid'=>$data['nid']]));
			}
		}
		$item = Db::name('news_result')->where('id',$id)->find();

		$this->assign('item',$item);

		$this->assign('map',$map);

		return $this->fetch('editanswer');
	}

	public function  relationItem()
	{
		$nid = input('nid');
		$item = Db::name("news_item")->where(['nid'=>$nid,'nceng'=>1])->field('id,iname')->order('ishunxu,id asc')->select();
		foreach($item as $k=>$val)
		{
			$item[$k]['couser']=Db::name('news_item')->where('nid',$val['id'])->field('id,iname,inextnum')->select();
		}
		$this->assign('nid',$nid);
		$this->assign('item',$item);
		return $this->fetch('relationitem');
	}

	public function ajaxitem()
	{
		$nid = input('nid');

		$inextnum=input('num');

		$res= Db::name('news_item')->where('id',$nid)->update(['inextnum'=>$inextnum]);
		if($res){
			return true;
		}else{
			return false;
		}
	}

	public function ajaxparam()
	{
		$nid = input('nid');
		$field=input('field');
		$inextnum=input('num');

		$res= Db::name('news_item')->where('id',$nid)->update([$field=>$inextnum]);
		if($res){
			return true;
		}else{
			return false;
		}
	}

	public function clearCache()
	{
		$id = input("aid");
		$filename='view-'.$id;
		$title="data_detail_".$id;
		$res = cache::rm($filename);
		cache::rm($title);
		if($res){
			return true;
		}else{
			return false;
		}
	}
	public function clearMore()
	{
		 
		$res = cache::store('redis')->rm("all_list");
		if($res){
			return true;
		}else{
			return false;
		}
	}
	public function clearPage()
	{
		 
		$res = system("cd /var/www/html/runtime/cache/ceshi_ && rm -rf *");
		 
		return true;
		
	}
	public function upField()
	{
		$aid = input('aid');
		$field=input('field');
		$val = input('val');

		$res= Db::name('news')->where('id',$aid)->update([$field=>$val]);
		if($res){
			return true;
		}else{
			return false;
		}
	}

}